.TH BACKUP.SH 1 LOCAL

.SH NAME
backup.sh - manage import and export data from modules

.SH SYNOPSIS
backup.sh 
.I [options]
.B -l

backup.sh
.I [options] <module>
.B import|export 
.I <destination>

.SH DESCRIPTION
.B backup.sh
can manage saved data of  
a set of modules with exporting (save) or importing (restore) the data.

With this script you can manage easily an application using multiples modules.

.SH OPTIONS
.TP 2
-D 
indicates to erase current data before importation.

.TP
-M <directory>
indicates the path of the modules files (see MODULES).
default : backup-modules

.TP
-E <file>
indicates the path of the environment file (see MODULES).
default: 
.I none

.TP
-l
show the list of modules.

.TP
-A nosys
action on all modules without
.B sys 
prefix.
Use it instead <module>.

.TP
-A allsys
action on all modules.
Use it instead <module>.

.TP
-v
show this 
.B backup.sh 
script's version.

.TP
-?
show the help message.

.SH COMMANDS
.TP 2
export <destination>
export the data of a module to a save under the destination directory.

.TP
import
import the data of a module from a previous save under the destination directory.

.SH MODULES
A module is a file describing how to export and import data of a component.
It's a script file with particular format (see below).
This file contains shell functions and description. 
An environment file can be used too for storing parameters or global functions.

.SS Environment file
It's an optional script launched on current shell (with 
.B . (dot) 
command). This file must be indicated with the 
.B -E 
option.

.SS Filename
This module's file must be in a directory (by default 
.I backup-modules
) with one of this format :
.I <number>_<name>
or
.I <number>_sys_<name>
were 
.TP 2
<number>
is a number. It indicates the order to export this module when 
.B -A 
option is used. When importing, the order is reversed.

.TP
<name>
is the name you want.
.TP
sys
is used for indicated a system module.

.SS System modules
A system module represents a component used globally on the system. Like 
.I ldap server
or
.I dns server 
that is when stopping this component has an impact on the machine and on the other applications.

.SS Format of the file
The file must contain description of the module (comment with NNNA) and 2 shell functions:
.TP 1
fn_export
.TP
fn_import.



Below, a template for the file:

 #!/bin/sh

 # ################################################# #
 # NNNA description describe here the component      #
 # NNNA author      you                              #
 # NNNA version     version of the script            #
 # ################################################# #

 fn_import()
 {
  if [ "$1" = '-D' ]; then
   #delete all data (erase)
   shift 1
   foobar
  fi
  #import the data from $1
  foo $1
  return 0 #ok
 }

 fn_export()
 {
   #export the data to $1
   faabar $1
   return 0 #ok
 }


.SH EXAMPLE
For instance if your application is composed by a website and database, you can create two module : 
.TP 2
one for the website
.TP
another for the database



.SS Website
The website is hosted in a virtualhost on a shared Apache server. 
So, the Apache is a system module.
We create the file 
.B backup-modules/20_sys_apache.
Below the content
 #!/bin/sh
 
 # ################################################# #
 # NNNA description apache web server                #
 # NNNA author      Example' author                  #
 # NNNA version     version 1.0                      #
 # ################################################# # 

 fn_export()
 {
  tar czvf $1/apache.tgz /var/nnnadminexample/mywebsite/htdocs
  return $?
 }
 
 fn_import()
 {
  if [ "$1" = '-D' ]; then
   #delete before remove not supported
   shift 1
  fi
  cd /
  tar xzf $1/apache.tgz 
  cd ->/dev/null
  return $?
 }

.SS Database
The database instance is launched on standalone but must be launch before Apache.
We create the file
.B backup-modules/10_database.
Below the content
 #!/bin/sh
 
 # ################################################# #
 # NNNA description database of the website          #
 # NNNA author      Example' author                  #
 # NNNA version     version 1.0                      #
 # ################################################# # 

 PG_DIR=/usr/lib/postgresql/9.1/bin
 PGDATA=/var/nnnadminexample/mydatabase
 
 fn_export()
 {
  su - postgres -c "PGDATA=$PGDATA;$PG_DIR/pg_dump -Ft -f $1/base.tar"
  return $?
 } 
 
 fn_import()
 {
  unset opt
  if [ "$1" = '-D' ]; then
   opt='-c'
  fi
  su - postgres -c "PGDATA=$PGDATA;$PG_DIR/pg_restore $opt -Ft <$1/base.tar"
  return $?
 }

.SS Exporting database only (to /backup directory)
backup.sh database export /backup

.SS Exporting all
backup.sh -A allsys export /backup

.SS Importing to apache from /backup directory
backup.sh apache import /backup

.SH AUTHOR
3Zen

.SH SEE ALSO
.BR admin.sh(1),
.BR check.sh(1),
.BR launch.sh(1)
